<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Image;
use think\Db;
use think\Loader;
use think\Paginator;
use app\admin\model\Place as Places;
class Place extends Common
{
    private $cModel;
    protected $targets = ["_blank" => "新标签页打开", "_self" => "本窗口打开"];

    public function __construct()
    {
        parent::__construct();
        $this->request = Request::instance();
        $this->cModel = new Places();
    }

    public function index()
    {

        $dataList = $this->cModel->selectList();
        $this->assign('dataList', $dataList);
        return $this->fetch();
    }

    public function create()
    {
       if (\request()->isPost()) {
        Db::startTrans();
        try {
            $data = input('post.');
            $data['introduce'] = json_encode(ch_json(array(
            'area' =>input('post.area'),
            'qixie' =>input('post.qixie'),
            'course'=>input('post.course')
            )));
            $result = $this->cModel->validate(CONTROLLER_NAME.'.add')->allowField(true)->save($data);
            if ($result) {
                Db::commit();
                return ajaxReturn(lang('action_success'),url('index'));
            } else {
                return ajaxReturn($this->cModel->getError());
            }

        } catch (Exception $e) {
            Db::rollback();
            return ajaxReturn($e->getMessage());
        }

       } else {
           return $this->fetch();
       }


    }

    public function edit()
    {
        if (request()->isPost()) {
            $data = input('post.');

            if ($data['actions'] == 'muser') {
               unset($data['actions']);
               $result = $this->cModel->validate(CONTROLLER_NAME.'.edit')->allowField(true)->save($data,$data['id']);

            } elseif($data['actions']=='images') {
                dump($data);die;
            }

            if($result){
                return ajaxReturn(lang('action_success'),url('index'));
            } else {
                return ajaxReturn($this->cModel->getError());
            }

        } else {
            $id = $this->request->param('id', 0, 'intval');
            $data = $this->cModel->get($id);
            $this->assign('data', $data);
            return $this->fetch();
        }

    }

    public function delete()
    {
       if (request()->isPost()){
            $id = input('id');
            if (isset($id) && !empty($id)){
                $id_arr = explode(',', $id);
                $where = [ 'id' => ['in', $id_arr] ];
                $result = $this->cModel->where($where)->delete();
                if ($result){
                    return ajaxReturn(lang('action_success'), url('index'));
                }else{
                    return ajaxReturn($this->cModel->getError());
                }
            }
        }
    }



    public function upload()
    {
        $images = $this->request->file('pic');

                dump($images);die;
        if($this->request->isPost()){
            //接收参数
            $images = $this->request->file('pic');

                dump($images);die;
            //计算md5和sha1散列值，TODO::作用避免文件重复上传
            $md5 = $images->hash('md5');
            $sha1= $images->hash('sha1');

            //判断图片文件是否已经上传
            $img = Db::name('place_pic')->where(['md5'=>$md5,'sha1'=>$sha1])->find();//我这里是将图片存入数据库，防止重复上传
            if(!empty($img)){
                return json(['status'=>1,'msg'=>'上传成功','data'=>['id'=>$img['id'],'img_url'=>$this->request->root(true).'/'.$img['pic']]]);
            }else{
                // 移动到框架应用根目录/public/uploads/image/目录下
                $imgPath = 'public' . DS . 'uploads' . DS . 'image';
                $info = $images->move(ROOT_PATH . $imgPath);
                $path = 'public/uploads/image/'.date('Ymd',time()).'/'.$info->getFilename();
                $data = [
                    'path' => $path ,
                    'md5' => $md5 ,
                    'sha1' => $sha1 ,
                    'status' => 1 ,
                    'create_time' => time() ,
                ];
                if($img_id=Db::name('place_pic')->insertGetId($data)){
                    return json(['status'=>1,'msg'=>'上传成功','data'=>['id'=>$img_id,'pic'=>$this->request->root(true).'/'.$path]]);
                }else{
                    return json(['status'=>0,'msg'=>'写入数据库失败']);
                }
            }
        }


    }

}